package view;

import bean.GoodInfo;
import bean.Member;
import bean.OrderInfo;
import dao.GoodInfoDao;
import dao.MemberDao;
import impl.GoodInfoDaoImpl;
import impl.MemberDaoImpl;
import login.Cashier;
import service.OrderAndDetailService;
import service.impl.OrderAndDetailServiceImpl;
import util.InputUtil;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @classname:BuyGoodView
 * @description:
 * @author: Mr.Z
 * @date: 2021/1/2 10:15
 * @version: 0.1
 * @since:1.8
 */
public class BuyGoodView {
    private static Map<Integer, GoodInfo> CARMAP = new LinkedHashMap<>(16);
    private static Map<String, GoodInfo> SEENGOOD = new LinkedHashMap<>(16);
    private static Map<Integer, Integer> PARAMS = new LinkedHashMap<>(16);
    private static BigDecimal money = new BigDecimal(0);


    private BuyGoodView() {
    }

    public static void start() {
        System.out.println("------------------------------");
        System.out.println("购物车");
        System.out.println("请选择请功能\n" +
                "1.添加商品 2.修改商品 3.删除购物车中的商品 4.展示购物车商品列表 5.浏览过的商品 6.返回主菜单");
        int num = InputUtil.inputInt();
        switch (num) {
            case 1:
                addCar();
                break;
            case 2:
                updateCar();
                break;
            case 3:
                deleteCar();
                break;
            case 4:
                showCar();
                break;
            case 5:
                seeCar();
                break;
            case 6:
                Cashier.showMethod();
                break;


        }

    }


    private static void addCar() {
        //只展示在库商品
        GoodInfoView.showAll();
        System.out.println("请选要购买的商品id");
        int id = InputUtil.inputInt();

        GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
        GoodInfo goodInfo = goodInfoDao.selectGoodById(id);
        System.out.println("您要购买的商品信息如下");
        System.out.println(goodInfo);
        System.out.println("确认购买么？1/0");
        int ch = InputUtil.inputInt();
        SEENGOOD.put(goodInfo.getGname(), goodInfo);
        if (ch == 1) {
            System.out.println("请输入要购买的数量");
            int num = InputUtil.inputInt();
            if (goodInfo.getNumber() < num) {
                System.out.println("商品库存不够请重新选择");
                choicess();
            }
            goodInfo.setNumber(num);
            System.out.println(goodInfo);
            //总钱数
            CARMAP.put(goodInfo.getId(), goodInfo);
            PARAMS.put(goodInfo.getId(), num);
            System.out.println("加入购物车成功");
        }
        choicess();
    }

    public static void caleMoney(int id,int mun){
        GoodInfoDao goodInfoDao = new GoodInfoDaoImpl();
        GoodInfo goodInfo = goodInfoDao.selectGoodById(id);
        Float price = goodInfo.getPrice();
        BigDecimal big1 = new BigDecimal(mun);
        BigDecimal big2 = new BigDecimal(price);
        BigDecimal big3 = big1.multiply(big2);
        money = money.add(big3);

    }


    private static void choicess() {
        System.out.println("继续添加商品/支付/返回上一级 (1/2/3)");
        int num = InputUtil.inputInt();
        switch (num) {
            case 1:
                addCar();
                break;
            case 2:
                pay();
                break;
            case 3:
                start();
                break;
        }
    }

    private static void seeCar() {
        System.out.println("浏览过的商品如下");
        SEENGOOD.forEach((k, v) -> {
            System.out.println("商品名:" + v.getGname());
        });
        start();
    }

    private static void showCar() {
        System.out.println("购物车的商品如下");
        CARMAP.forEach((k, v) -> {
            System.out.println("商品" + k + ",商品名:" + v.getGname() + "————购买数量" + v.getNumber());
        });
        System.out.println(money);
        start();
    }

    private static void deleteCar() {
        System.out.println("购物车的商品如下");
        CARMAP.forEach((k, v) -> {
            System.out.println("商品" + k + ",商品名:" + v.getGname() + "————购买数量" + v.getNumber());
        });
        System.out.println("请选择要删除的商品排序数字");
        int num = InputUtil.inputInt();
        CARMAP.remove(num);
        System.out.println("删除成功");
        System.out.println("是否继续删除?y/n");
        String str = InputUtil.inputNextStr();
        if ("y".equals(str)) {
            deleteCar();
        }
        start();
    }

    private static void updateCar() {
        System.out.println("购物车的商品如下");
        CARMAP.forEach((k, v) -> {
            System.out.println("商品" + k + ",商品名:" + v.getGname() + "————购买数量" + v.getNumber());
        });
        System.out.println("请选择要修改到的商品排序数字");
        int num = InputUtil.inputInt();
        GoodInfo goodInfo = CARMAP.get(num);
        System.out.println("请输入修改商品的数量");
        int nums = InputUtil.inputInt();
        goodInfo.setNumber(nums);
        start();
    }


    private static void pay() {
        System.out.println("是否使用会员卡支付1/0");
        OrderInfo orderInfo = new OrderInfo();
        int choice = InputUtil.inputInt();
        if (choice == 1) {
            System.out.println("请输入会员卡号");
            int id = InputUtil.inputInt();
            MemberDao memberDao = new MemberDaoImpl();
            Member member = memberDao.selectMemberById(id);
            BigDecimal balance = member.getBalance();
            //会员的剩余金额和消费的金钱做对比-1为少0正好1比支付金额多
            PARAMS.forEach((k,v)->{
                caleMoney(k,v);
            });
            int i = balance.compareTo(money);
            if (i==-1) {
                System.out.println("余额不足 请充值");
                pay();
            }
            orderInfo.setMid(id);
            orderInfo.setTotal(money);
            orderInfo.setPaytype(0);
        } else {
            System.out.println("使用现金支付");
            orderInfo.setMid(-1);
            orderInfo.setPaytype(0);
        }

        OrderAndDetailService orderAndDetailService = new OrderAndDetailServiceImpl();
        orderAndDetailService.addOrder(PARAMS, orderInfo);

        System.out.println("支付成功");
        //支付成功清空购物车
        SEENGOOD.clear();
        //支付成功清空浏览记录
        CARMAP.clear();
        //清空参数
        PARAMS.clear();
        money = new BigDecimal(0);
        start();
        choicess();
    }
}
